package com.eqdd.yiqidian.utils;

import com.eqdd.yiqidian.ui.select.RichTextResult;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lv on 17-8-24.
 */

public class RichTextUtil {
    public static RichTextResult getRichText(String content, List list, String field) {
        RichTextResult richTextResult = new RichTextResult();
        try {
            richTextResult.setContent(content);
            ArrayList<LocalMedia> localMedia = new ArrayList<>();
            if (list != null && list.size() > 0) {
                for (Object s : list) {
                    localMedia.add(new LocalMedia(getField(s, field), 0, PictureMimeType.ofImage(), PictureConfig.IMAGE));
                }
            }
            richTextResult.setSelectMedia(localMedia);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return richTextResult;
    }

    public static RichTextResult getRichText(String content, List<String> list) {
        RichTextResult richTextResult = new RichTextResult();
        try {
            richTextResult.setContent(content);
            ArrayList<LocalMedia> localMedia = new ArrayList<>();
            if (list != null && list.size() > 0) {
                for (String s : list) {
                    localMedia.add(new LocalMedia(s, 0, PictureMimeType.ofImage(), PictureConfig.IMAGE));
                }
            }
            richTextResult.setSelectMedia(localMedia);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return richTextResult;
    }

    private static String getField(Object o, String field) throws Exception {
        Field declaredField = o.getClass().getDeclaredField(field);
        declaredField.setAccessible(true);
        return (String) declaredField.get(o);
    }
}
